function userConfig(app,db) {

     //获取用户信息
     app.post('/getuser', async function (req, res, next) {
        db.serialize(function () {
            let data = {
                msg:'',
                code:200,
                id:0,
                name:'',
                email:''
            }
            db.all('SELECT * FROM user where id = '+req.session.user.id,
            function(err,dbres){
                if (!err) {
                    data.id = dbres[0].id
                    data.name = dbres[0].name
                    data.email = dbres[0].email
                    res.send(data)
                }else{
                    data.code = 300
                    data.msg = '查询失败'
                    res.send(data)
                }
                next()
            })
            
        })
    })
    //登录
    app.post('/loginuser', async function (req, res, next) {
        let msg = req.body
        db.serialize(function () {
            let data = {
                msg:'',
                code:200,
                id:0,
                name:'',
                email:''
            }
            db.all('SELECT * FROM user where email = '+"'"+msg.email+"'",
            function(err,dbres){
                if (!err && dbres.length > 0 && dbres[0].psw === msg.psw) {
                    data.id = dbres[0].id
                    data.name = dbres[0].name
                    data.email = dbres[0].email
                    req.session.user = dbres[0]
                    res.send(data)
                }else{
                    console.log(err)
                    data.code = 300
                    data.msg = '登录失败'
                    res.send(data)
                }
                next()
            })
        })
    })

    //修改
    app.post('/updateuser', async function (req, res, next) {
        let msg = req.body
        let data = {
            msg:'',
            code:200,
            id:0,
            name:'',
            email:''
        }
        if ( msg.psw !== req.session.user.psw) {
            data.code = 400
            data.msg = '密码错误'
            res.send(data)
            next()
            return
        }
        db.serialize(function () {
            if (msg.newpsw && msg.newpsw !== '') {
                console.log('新密码',msg.newpsw)
                db.run("update user set psw = "+"'"+msg.newpsw+"'"+" where id = "+req.session.user.id,
                    function(err){
                        if (err) {
                            console.log(err)
                        }
                    }
                )
            }
            db.run("update user set name = "+"'"+msg.name+"'"+" where id = "+req.session.user.id,
                function(err){
                    if (err) {
                        console.log(err)
                    }
                }
            )
            db.all("SELECT * FROM user where id = "+req.session.user.id,
            function(err,dbres){
                if (err) {
                    data.code = 400
                    data.msg = '错误'
                    res.send(data)
                }else{
                    req.session.user = dbres[0]
                    data.id = dbres[0].id
                    data.name = dbres[0].name
                    data.email = dbres[0].email
                    res.send(data)
                }
                next()
            })
            
        })
    })

    //注册
    app.post('/newuser', async function (req, res, next) {
        let msg = req.body
        db.serialize(function () {
            let data = {
                msg:'',
                code:200,
                id:0,
                name:'',
                email:''
            }
            db.all('SELECT * FROM user where email = '+"'"+msg.email+"'",
            function(err,dbres){
                if (!err && dbres.length > 0 ) {
                    data.code = 300
                    data.msg = '邮箱已经存在，注册失败'
                    res.send(data)
                    next()
                }else{
                    if (err) {
                        data.code = 300
                        data.msg = '注册失败'
                        res.send(data)
                        next()
                    }else{
                        db.run(
                            "insert into user(name,email,psw,photo)  values(?,?,?,?)",
                            msg.name,
                            msg.email,
                            msg.psw,
                            null,
                            function(err,res){
                            }
                        )
                        db.all('SELECT * FROM user where email = '+"'"+msg.email+"'",
                        function(err,dbres){
                            if (!err && dbres.length > 0) {
                                data.id = dbres[0].id
                                data.name = dbres[0].name
                                data.email = dbres[0].email
                                req.session.user = dbres[0]
                                res.send(data)
                            }else{
                                data.code = 300
                                data.msg = '注册失败'
                                res.send(data)
                            }
                            next()
                        })
                    }
                }
            })
        })
    })

    app.post('/getuserlist', async function (req, res, next) {
        let msg = req.body
        db.serialize(function () {
            let data = {
                msg:'',
                code:200,
                userlist:[]
            }
            db.all('SELECT * FROM user',
            function(err,dbres){
                if (!err) {
                    data.userlist = dbres
                    for (let index = 0; index < data.userlist.length; index++) {
                        data.userlist[index].mymodeList = []
                        db.all('SELECT * FROM mymode where userid = '+data.userlist[index].id,
                        function(err,sqres){
                            if (!err) {
                                data.userlist[index].mymodeList = sqres
                            }
                        })
                    }
                    db.all('SELECT * FROM mymode where userid = '+req.session.user.id,
                    function(err,sqres){
                        res.send(data)
                        next()
                    })
                }else{
                    data.code = 300
                    data.msg = '查询失败'
                    res.send(data)
                    next()
                }
            })
        })
    })

    app.post('/killuser', async function (req, res, next) {
        let msg = req.body
        let data = {
            msg:'',
            code:200,
        }
        if (req.session.user.id !== 1) {
            data.code = 300
            data.msg = '你没有权限！'
            res.send(data)
            next()
            return
        }
        if (msg.userid === 1 || msg.userid === 0 || msg.userid === '1' || msg.userid === '0') {
            data.code = 300
            data.msg = '默认用户不能删除'
            res.send(data)
            next()
            return
        }
        if (msg.userid === undefined || msg.userid === null || msg.userid === '' ) {
            data.code = 300
            data.msg = '???'
            res.send(data)
            next()
            return
        }
        db.serialize(function () {
            db.run('delete from indexmode where userid = '+msg.userid,
            function(err){
                if (err) {
                    console.log(err)
                }
            })
            db.run('delete from mymode where userid = '+msg.userid,
            function(err){
                if (err) {
                    console.log(err)
                }
            })
            db.run('delete from user where id = '+msg.userid,
            function(err){
                if (err) {
                    console.log(err)
                }
                res.send(data)
                next()
            })
        })
    })
}

module.exports ={userConfig}